package com.warehouse.service;

import com.warehouse.dao.CargoDao;
import com.warehouse.dao.impl.CargoDaoImpl;
import com.warehouse.entity.Cargo;
import com.warehouse.utils.Page;

import java.util.List;

public class CargoService {
    private CargoDao cargoDao=new CargoDaoImpl();


    public Page<Cargo> queryPage(int start, int pageSize) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = cargoDao.count();//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Cargo> list = cargoDao.allPage(startPage, pageSize);
        return new Page<Cargo>(start, totalPage, pageSize, list);
    }

    public int add(Cargo cargo) {
        return cargoDao.add(cargo);
    }

    public int update(Cargo cargo) {
        return cargoDao.update(cargo);
    }


    public int delete(int id) {
        return cargoDao.delete(id);
    }

    public Cargo byId(int id) {
        return cargoDao.byId(id);
    }

    public Page<Cargo> byCardNum(int start, int pageSize,Integer typeId) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = cargoDao.countByType(typeId);//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Cargo> cargos = cargoDao.byType(startPage, pageSize, typeId);
        return new Page<Cargo>(start, totalPage, pageSize, cargos);
    }

}
